package com.examine.serviceImpl;

import com.examine.bean.News;
import com.examine.bean.User;
import com.examine.dao.MyApprovalMapper;
import com.examine.dao.NewsMapper;
import com.examine.service.MessageService;
import com.examine.service.NewsService;
import com.examine.utils.DataConfig;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsServiceImpl implements NewsService {
    private static SqlSession session = null;
    private static NewsMapper newsMapper = null;
    private News news = null;
    private List<News> newsList = null;
    private int count = 0;

    public static void dataConfig(){
        try{
            session = DataConfig.getSession();
            newsMapper = session.getMapper(NewsMapper.class);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        dataConfig();

        try{
            count = newsMapper.deleteByPrimaryKey(id);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public int insertSelective(News record) {
        dataConfig();

        try{
            count = newsMapper.insertSelective(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public News selectByPrimaryKey(String id) {
        dataConfig();

        try{
            news = newsMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return news;
    }

    @Override
    public int updateByPrimaryKeySelective(News record) {
        dataConfig();

        try{
            count = newsMapper.updateByPrimaryKeySelective(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    @Override
    public List<News> getAllNewsList(News record) {
        dataConfig();
        try{
            newsList = newsMapper.getAllNewsList(record);
            for(News news1 : newsList){

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return newsList;
    }

    @Override
    public int queryCountLike(News record) {
        dataConfig();

        try{
            count = newsMapper.queryCountLike(record);
            if (count > 0){
                session.commit();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

}
